<script setup>
import { ref } from 'vue'
import { adminLogin } from "@/api/admin";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";

const router = useRouter();
const username = ref('admin01')
const password = ref('xjtuse')
const rememberMe = ref(false)
const showPassword = ref(false)
const isLoading = ref(false)

// 切换密码可见性
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}

const handleLogin = () => {
  if (!username.value.trim()) {
    ElMessage.warning('请输入账号')
    return
  }
  if (!password.value) {
    ElMessage.warning('请输入密码')
    return
  }

  isLoading.value = true
  const admin = {
    username: username.value,
    password: password.value
  }

  adminLogin(admin).then(res => {
    if (res.data) {
      router.push("/admin/audit")
    } else {
      ElMessage({
        message: '登录失败，请检查账号或密码',
        type: 'error'
      })
    }
  }).catch(error => {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请稍后再试')
  }).finally(() => {
    isLoading.value = false
  })
}
</script>

<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="login-bg">
      <div class="bg-pattern"></div>
      <div class="bg-gradient"></div>
    </div>

    <div class="login-wrapper">
      <!-- 左侧品牌区 -->
      <div class="login-brand">
        <div class="brand-content">
          <div class="logo">
            <!-- 与首页一致的对称式logo：对话气泡+单星星 -->
            <svg
                class="logo-icon"
                viewBox="0 0 48 48"
                fill="none"
            >
              <!-- 外层对称气泡形状 -->
              <path d="M12 24C12 15.1634 19.1634 8 28 8C36.8366 8 44 15.1634 44 24C44 32.8366 36.8366 40 28 40C23.5817 40 19.5291 38.2426 16.5858 35.2993L12 40V24Z"
                    stroke="#FFFFFF" stroke-width="2.5" fill="rgba(255,255,255,0.1)"/>

              <!-- 中央评分星星 -->
              <path d="M24 14L26.5 22L34 22L28 26.5L30 34L24 30L18 34L20 26.5L14 22L21.5 22L24 14Z"
                    fill="#FFFFFF"/>
            </svg>
            <span class="logo-text">点评通管理平台</span>
          </div>

          <h3 class="brand-slogan">专业管理，高效运营</h3>
          <p class="brand-desc">一站式管理系统，助力平台高效运营</p>

          <!-- 管理平台优势 -->
          <div class="brand-features">
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fa fa-shield"></i>
              </div>
              <div class="feature-text">安全可靠的权限管理</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fa fa-line-chart"></i>
              </div>
              <div class="feature-text">全面的数据分析工具</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fa fa-cogs"></i>
              </div>
              <div class="feature-text">便捷的系统配置中心</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-form-container">
        <div class="login-form-wrapper">
          <div class="form-header">
            <h2 class="form-title">管理员登录</h2>
            <p class="form-subtitle">请使用管理员账号登录系统</p>
          </div>

          <form class="login-form" @submit.prevent="handleLogin">
            <div class="form-group">
              <label for="admin-account" class="form-label">账号</label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-user-circle"></i>
                </span>
                <input
                    type="text"
                    id="admin-account"
                    v-model="username"
                    placeholder="请输入账号"
                    required
                    class="form-input"
                >
              </div>
            </div>

            <div class="form-group">
              <label for="admin-password" class="form-label">密码</label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-lock"></i>
                </span>
                <input
                    :type="showPassword ? 'text' : 'password'"
                    id="admin-password"
                    v-model="password"
                    placeholder="请输入密码"
                    required
                    class="form-input"
                >
                <button
                    type="button"
                    class="toggle-password"
                    @click="togglePasswordVisibility"
                    aria-label="切换密码可见性"
                >
                  <i :class="showPassword ? 'fa fa-eye' : 'fa fa-eye-slash'"></i>
                </button>
              </div>
            </div>

            <div class="form-options">
              <div class="remember-me">
                <label class="custom-checkbox">
                  <input type="checkbox" v-model="rememberMe">
                  <span class="checkmark"></span>
                  <span class="label-text">记住我</span>
                </label>
              </div>
              <a href="/forgotPassword" class="forgot-password">忘记密码?</a>
            </div>

            <button
                type="submit"
                class="login-button"
                :disabled="isLoading"
            >
              <span v-if="!isLoading">登录</span>
              <span v-else class="loading-state">
                <i class="fa fa-spinner fa-spin"></i> 登录中...
              </span>
            </button>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
// 颜色变量
$primary: #1E40AF; // 与商家端保持统一的主色调
$primary-light: #3B82F6;
$primary-glass: rgba(30, 64, 175, 0.1);
$text-primary: #1E293B;
$text-secondary: #64748B;
$text-tertiary: #94A3B8;
$bg-light: #F8FAFC;
$white: #FFFFFF;
$shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
$shadow-hover: 0 8px 30px rgba(0, 0, 0, 0.12);
$transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

// 登录容器
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  padding: 20px;
}

// 背景装饰
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;

  .bg-pattern {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%231E40AF' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
  }

  .bg-gradient {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, $bg-light 0%, $white 100%);
  }
}

// 登录包装器
.login-wrapper {
  display: flex;
  width: 90%;
  max-width: 1200px;
  min-height: 600px;
  background-color: $white;
  border-radius: 24px;
  box-shadow: $shadow-hover;
  position: relative;
  z-index: 1;
  overflow: hidden;

  @media (max-width: 992px) {
    flex-direction: column;
    min-height: auto;
    margin: 20px 0;
  }
}

// 品牌区域
.login-brand {
  flex: 1;
  background: linear-gradient(135deg, $primary 0%, #0F172A 100%);
  padding: 60px;
  display: flex;
  align-items: center;
  color: $white;

  @media (max-width: 992px) {
    padding: 40px;
  }

  .brand-content {
    width: 100%;

    .logo {
      display: flex;
      align-items: center;
      gap: 14px;
      margin-bottom: 40px;

      .logo-icon {
        width: 48px;
        height: 48px;
        transition: transform 0.3s ease, filter 0.3s ease;

        &:hover {
          transform: rotate(5deg) scale(1.05);
          filter: drop-shadow(0 0 8px rgba(255, 255, 255, 0.3));
        }
      }

      .logo-text {
        font-size: 28px;
        font-weight: 700;
        letter-spacing: -0.5px;
      }
    }

    .brand-slogan {
      font-size: 26px;
      font-weight: 600;
      margin-bottom: 16px;
    }

    .brand-desc {
      font-size: 16px;
      line-height: 1.6;
      color: rgba(255, 255, 255, 0.8);
      margin-bottom: 50px;
      max-width: 400px;
    }

    // 品牌特点
    .brand-features {
      display: grid;
      grid-template-columns: 1fr;
      gap: 20px;

      .feature-item {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 16px;
        background-color: rgba(255, 255, 255, 0.05);
        border-radius: 10px;
        transition: $transition;

        &:hover {
          background-color: rgba(255, 255, 255, 0.1);
          transform: translateX(5px);
        }

        .feature-icon {
          width: 40px;
          height: 40px;
          background-color: rgba(255, 255, 255, 0.1);
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 18px;
        }

        .feature-text {
          font-size: 16px;
          color: rgba(255, 255, 255, 0.9);
        }
      }
    }
  }
}

// 表单区域
.login-form-container {
  flex: 1;
  padding: 60px;
  display: flex;
  align-items: center;
  justify-content: center;

  @media (max-width: 992px) {
    padding: 40px 30px;
  }

  .login-form-wrapper {
    width: 100%;
    max-width: 400px;

    .form-header {
      margin-bottom: 40px;

      .form-title {
        font-size: 32px;
        font-weight: 700;
        color: $text-primary;
        margin-bottom: 8px;
        position: relative;

        &::after {
          content: '';
          display: block;
          width: 50px;
          height: 4px;
          background: $primary;
          margin: 12px 0 0;
          border-radius: 2px;
        }
      }

      .form-subtitle {
        font-size: 16px;
        color: $text-secondary;
      }
    }
  }
}

// 表单样式
.login-form {
  width: 100%;

  .form-group {
    margin-bottom: 24px;

    .form-label {
      display: block;
      font-size: 14px;
      font-weight: 500;
      color: $text-primary;
      margin-bottom: 8px;
    }

    .form-input-wrapper {
      position: relative;
      width: 100%;

      .input-icon {
        position: absolute;
        left: 16px;
        top: 50%;
        transform: translateY(-50%);
        color: $text-tertiary;
        font-size: 18px;
        transition: $transition;
      }

      .form-input {
        width: 100%;
        padding: 14px 14px 14px 50px;
        border: 1px solid #E2E8F0;
        border-radius: 8px;
        font-size: 16px;
        color: $text-primary;
        transition: $transition;
        background-color: $bg-light;

        &:focus {
          outline: none;
          border-color: $primary;
          box-shadow: 0 0 0 4px $primary-glass;
          background-color: $white;

          ~ .input-icon {
            color: $primary;
          }
        }

        &::placeholder {
          color: $text-tertiary;
        }
      }

      .toggle-password {
        position: absolute;
        right: 14px;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        color: $text-tertiary;
        cursor: pointer;
        transition: $transition;
        width: 36px;
        height: 36px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;

        &:hover {
          color: $primary;
          background-color: $primary-glass;
        }
      }
    }
  }

  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;

    .remember-me {
      display: flex;
      align-items: center;

      .custom-checkbox {
        display: flex;
        align-items: center;
        cursor: pointer;

        input {
          position: absolute;
          opacity: 0;
          cursor: pointer;
          height: 0;
          width: 0;
        }

        .checkmark {
          position: relative;
          height: 20px;
          width: 20px;
          border: 1px solid #E2E8F0;
          border-radius: 4px;
          transition: $transition;
          background-color: $bg-light;

          &::after {
            content: "";
            position: absolute;
            display: none;
            left: 6px;
            top: 2px;
            width: 5px;
            height: 10px;
            border: solid $white;
            border-width: 0 2px 2px 0;
            transform: rotate(45deg);
          }
        }

        input:checked ~ .checkmark {
          background-color: $primary;
          border-color: $primary;
        }

        input:checked ~ .checkmark::after {
          display: block;
        }

        .label-text {
          margin-left: 8px;
          font-size: 14px;
          color: $text-secondary;
        }
      }
    }

    .forgot-password {
      font-size: 14px;
      color: $primary;
      text-decoration: none;
      transition: $transition;

      &:hover {
        color: $primary-light;
        text-decoration: underline;
      }
    }
  }

  .login-button {
    width: 100%;
    padding: 14px;
    background-color: $primary;
    color: $white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    transition: $transition;
    box-shadow: 0 4px 12px rgba(30, 64, 175, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;

    &:hover:not(:disabled) {
      background-color: $primary-light;
      transform: translateY(-2px);
      box-shadow: 0 6px 16px rgba(30, 64, 175, 0.3);
    }

    &:disabled {
      background-color: #E2E8F0;
      cursor: not-allowed;
      box-shadow: none;
    }

    .loading-state {
      display: flex;
      align-items: center;
      gap: 8px;
    }
  }
}
</style>